<?php
/**
 * 此模型是用来连接数据库xiche的，
 * 
 */ 
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class XcUserXiche extends Model
{
    protected $connection = 'mysql_master';
    protected $table = 'xc_user';
    protected $guarded = ['id'];



    public function userRecharge()
    {
        return $this->hasMany('App\Models\XcUserRecharge', 'user_id', 'id')->where(['pay_status' => 2]);
    }

    public function siteOrder()
    {
        return $this->hasMany('App\Models\XcSiteOrder', 'openid', 'openid')->where('points', '>', 0);
    }

    public function updateMoneyAndPoints($id, $money = 0, $points = 0, $pay_money = 0)
    {
        if (!$money && !$points) {
            return;
        }

        return $this->where('id', $id)->update([
            'money' => DB::raw('money + ' . $money),
            'userpoints' => DB::raw('userpoints + ' . $points),
            'money_history' => DB::raw('money_history + ' . $pay_money),
        ]);
    }

    public function updateNum($id, $fast_num = 0, $fine_num = 0, $pay_money = 0)
    {
        if (!$fast_num && !$fine_num) {
            return;
        }

        return $this->where('id', $id)->update([
            'fast_washing_num' => DB::raw('fast_washing_num + ' . $fast_num),
            'fine_washing_num' => DB::raw('fine_washing_num + ' . $fine_num),
            'money_history' => DB::raw('money_history + ' . $pay_money),
        ]);
    }

    /*
     *会员卡剩余天数计算
     */
    static function plusData(array $user)
    {
        $data = 0;
        $time = time();
        if($user['fast_member_exp'] && $user['fine_member_exp']){
            $user['fast_member_exp'] = strtotime($user['fast_member_exp']);
            $user['fine_member_exp'] = strtotime($user['fine_member_exp']);
            if($user['fast_member_exp'] > $user['fine_member_exp']){
                $data = ceil(($user['fast_member_exp'] - $time) / 86400);
            } else {
                $data = ceil(($user['fine_member_exp'] - $time) / 86400);
            }
        } else {
            if($user['fast_member_exp']){
                $user['fast_member_exp'] = strtotime($user['fast_member_exp']);
                $data = ceil(($user['fast_member_exp'] - $time) / 86400);
            }
            if($user['fine_member_exp']){
                $user['fine_member_exp'] = strtotime($user['fine_member_exp']);
                $data = ceil(($user['fine_member_exp'] - $time) / 86400);
            }
        }
        return $data > 0 ? $data : 0;
    }
}
